const {
  GiftShopModel
} = require('../../../../utils/giftshop-model.js');
var giftShopModel = new GiftShopModel();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLayerShow: false,
    type: 1, //1.售后申请 2.处理中
    searchKeyWord: '', //搜索关键字
    orderTime: 0, //下单时间 0.全部 1.一月内 2.一个月至三个月 3.三个月至六个月 4.六个月至一年 5.一年以上
    pageIndex: 1,
    pageSize: 20,
    pageCount: 0,
    loadText: "",
    serviceData: [], //申请
    isHints: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function (options) {
    this.setData({
      pageIndex: 1,
      pageCount: 0,
      loadText: "",
      serviceData: [],
      isHints: false,
    }, () => {
      this.searchServiceOrder()
    })
  },

  /**
   * tab切换
   */
  selectType(e) {
    let orderType = parseInt(e.currentTarget.dataset.type)
    if(orderType==1){//1.售后申请 2.处理中
      wx.reportAnalytics('gift_after_sale');
    }else if(orderType==2){
      wx.reportAnalytics('gift_service');
    }
    this.setData({
      type: orderType,
      searchKeyWord: '', //搜索关键字
      orderTime: 0, //下单时间
      pageIndex: 1,
      pageCount: 0,
      loadText: "",
      serviceData: [],
      isHints: false,
    }, () => {
      this.searchServiceOrder()
    })
  },

  /**
   * 查询服务单
   */
  searchServiceOrder() {
    if (this.data.type == 1) { //售后申请
      this.getServiceList()
    } else if (this.data.type == 2) { //处理中
      this.getDisposeList()
    }
  },

  /**
   * 选择下单时间
   */
  selectOrderData(e) {
    let id = parseInt(e.currentTarget.dataset.id)
    this.setData({
      orderTime: id,
    })
  },

  /**
   * 点击筛选按钮
   */
  setFilter() {
    wx.reportAnalytics('gift_service_filtrate');
    this.setData({
      isLayerShow: true
    })
  },

  /**
   * 点击重置按钮
   */
  setReset() {
    wx.reportAnalytics('gift_service_ordertime_reset');
    this.setData({
      orderTime: 0, //下单时间
      isLayerShow: false,
      searchKeyWord: '', //搜索关键字
      pageIndex: 1,
      pageCount: 0,
      loadText: "",
      serviceData: [], //申请
      isHints: false,
    }, () => {
      this.searchServiceOrder()
    })
  },

  /**
   * 点击确定按钮
   */
  setConfirm() {
    wx.reportAnalytics('gift_service_ordertime_confirm');
    this.setData({
      isLayerShow: false,
      pageIndex: 1,
      pageCount: 0,
      loadText: "",
      serviceData: [], //申请
      isHints: false,
    }, () => {
      this.searchServiceOrder()
    })
  },


  /**
   * 搜索
   */
  setValueName: function (e) {
    var value = e.detail.value;
    let that = this;
    that.setData({
      searchKeyWord: value,
    })
  },

  /**
   * 点击搜索
   */
  getSearchOrder() {
    wx.reportAnalytics('gift_service_search');
    this.setData({
      orderTime: 0, //下单时间
      pageIndex: 1,
      pageCount: 0,
      loadText: "",
      serviceData: [], //申请
      isHints: false,
    }, () => {
      this.searchServiceOrder()
    })
  },

  /**
   * 删除搜索名称
   */
  delectSearchName() {
    this.setData({
      searchKeyWord: '',
      orderTime: 0, //下单时间
      pageIndex: 1,
      pageCount: 0,
      loadText: "",
      serviceData: [], //申请
      isHints: false,
    }, () => {
      this.searchServiceOrder()
    })
  },

  /**
   * 售后申请
   */
  getServiceList: function () {
    let that = this;
    var param = {
      merchantId: wx.getStorageSync('merchantId'), //商户ID
      searchKeyWord: that.data.searchKeyWord, //搜索关键字
      orderTime: that.data.orderTime, //下单时间
      offset: (that.data.pageIndex - 1) * that.data.pageSize,
      limit: that.data.pageSize
    }
    giftShopModel.userOrderServiceList(param).then(data => {
      if (data) {
        let pageCountTotal = data.totalCount % that.data.pageSize == 0 ? (data.totalCount / that.data.pageSize) : (parseInt(data.totalCount / that.data.pageSize) + 1);
        that.setData({
          pageCount: pageCountTotal
        })
        if (data.totalCount > 0) {
          // 将新一页的数据添加到原数据后面
          var orderDataList = that.data.serviceData;
          var newOrderData = orderDataList.concat(data.data);
          that.setData({
            isLoadData: false,
            isHints: false,
            serviceData: newOrderData
          })
        } else {
          that.setData({
            loadText: "",
            isLoadData: false,
            isHints: true,
            serviceData: []
          })
        }
      }
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 处理中
   */
  getDisposeList: function () {
    let that = this;
    var param = {
      merchantId: wx.getStorageSync('merchantId'), //商户ID
      searchKeyWord: that.data.searchKeyWord, //搜索关键字
      orderTime: that.data.orderTime, //下单时间
      offset: (that.data.pageIndex - 1) * that.data.pageSize,
      limit: that.data.pageSize
    }
    giftShopModel.userServiceOrderList(param).then(data => {
      if (data) {
        let pageCountTotal = data.totalCount % that.data.pageSize == 0 ? (data.totalCount / that.data.pageSize) : (parseInt(data.totalCount / that.data.pageSize) + 1);
        that.setData({
          pageCount: pageCountTotal
        })
        if (data.totalCount > 0) {
          // 将新一页的数据添加到原数据后面
          var orderDataList = that.data.serviceData;
          var newOrderData = orderDataList.concat(data.data);
          that.setData({
            isLoadData: false,
            isHints: false,
            serviceData: newOrderData
          })
        } else {
          that.setData({
            loadText: "",
            isLoadData: false,
            isHints: true,
            serviceData: []
          })
        }
      }
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 滚动到底部/右边时触发
   */
  scrollEnd: function () {
    this.setData({
      pageIndex: this.data.pageIndex + 1,
      isLoadData: true,
      loadText: "加载中..."
    }, function () {
      if (this.data.pageIndex > this.data.pageCount) {
        this.setData({
          isLoadData: true,
          loadText: "已经到底了"
        });
      } else {
        this.searchServiceOrder()
      }
    })
  },

  /**
   * 取消换货
   */
  cancelService(e) {
    wx.reportAnalytics('gift_service_cancelapply');
    let that = this;
    let code = e.currentTarget.dataset.code //订单号
    wx.showModal({
      title: '是否取消换货申请？',
      cancelColor: '#333333', //取消文字的颜色
      confirmColor: '#333333', //确定文字的颜色
      confirmText: '确认',
      success(res) {
        if (res.confirm) {
          giftShopModel.serviceOrderCancel({
            merchantId: wx.getStorageSync('merchantId'), //商户ID
            serviceOrderCode: code, //服务单号
          }).then(data => {
            that.setData({
              serviceData: [], //申请
              isHints: false,
              pageCount: 0,
              pageIndex: 1,
              pageCount: 0,
              loadText: "",
            }, () => {
              that.searchServiceOrder();
            })
          }).catch(err => {
            wx.showToast({
              title: `${err.error}`,
              icon: 'none',
              duration: 2000
            })
          })
        } else if (res.cancel) {}
      }
    })
  },


  /**
   * 跳转到服务单详情页面
   */
  goServiceDetail: function (e) {
    let code = e.currentTarget.dataset.code //服务单号
    if (code) {
      wx.reportAnalytics('gift_service_detail');
      wx.navigateTo({
        url: '/pages/giftShop/serviceOrder/serviceDetails/index?serviceOrderCode=' + code
      })
    }
  },

  /**
   * 换货
   */
  setAfterSale: function (e) {
    wx.reportAnalytics('gift_service_apply');
    let orderCode = e.currentTarget.dataset.code
    let partnerSign = e.currentTarget.dataset.id
    let skuId = e.currentTarget.dataset.sku
    let quantity = e.currentTarget.dataset.num
    giftShopModel.serviceOrderPreCheck({
      merchantId: wx.getStorageSync('merchantId'), //商户ID
      orderCode: orderCode, //订单号
      partnerSign: partnerSign, //第三方id
      skuId: skuId, //商品sku
      quantity: quantity, //换货数量
    }).then(data => {
      wx.navigateTo({
        url: `/pages/giftShop/serviceOrder/afterType/index?currency=${encodeURIComponent(JSON.stringify(data))}`
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })

  }
})